package org.testis.gui.mvc.model.testdesign;

import org.testis.gui.mvc.model.*;
import org.testis.gui.mvc.controller.testdesign.DataObjectController;
import org.testis.gui.mvc.utilities.ParameterTableModel;
import java.util.HashMap;

import org.testis.utils.parametermodel.ParameterModel;

/**
 *
 * @author Cahit Emir Erel
 */
public class DataObjectModel extends AbstractModel {

    private Boolean m_editableKey = false;
    private Boolean m_dataObjectVisibility = false;
    private String m_name = "";
    private String m_modulType = "";
    private String m_key = "";
    private String m_value = "";
    private String m_expectedResult = "";
    private ParameterTableModel m_model;
    private String m_lastWarning = "";

    public DataObjectModel() {
        super();
    }

    public DataObjectModel(ParameterTableModel model) {
        super();
        setModel(model);
    }

    public String getName() {
        return m_name;
    }

    public void setName(String name) {
        if (name == null) {
            name = "";
        }
        String oldName = m_name;
        m_name = name;
        firePropertyChange(DataObjectController.CHANGE_NAME,
                oldName,
                name);
    }

    public void setDataObjectVisibility(Boolean dataObjectVisibility) {
        if (dataObjectVisibility != null) {
            Boolean oldDataObjectVisibility = m_dataObjectVisibility;
            m_dataObjectVisibility = dataObjectVisibility;
            firePropertyChange(DataObjectController.SHOW_ADD_DATA_OBJECT,
                    oldDataObjectVisibility,
                    dataObjectVisibility);
        }
    }

    public Boolean getDataObjectVisibility() {
        return m_dataObjectVisibility;
    }

    public void setEditableKey(Boolean editable) {
        if (editable != null) {
            Boolean oldEditableKey = m_editableKey;
            m_editableKey = editable;
            firePropertyChange(DataObjectController.EDITABLE_KEY,
                    oldEditableKey,
                    editable);
        }
    }

    public Boolean getEditableKey() {
        return m_editableKey;
    }

    public String getModulType() {
        return m_modulType;
    }

    public void setModulType(String modulType) {
        if (modulType == null) {
            modulType = "";
        }
        String oldModulType = m_modulType;
        m_modulType = modulType;
        firePropertyChange(DataObjectController.CHANGE_MODUL_TYPE,
                oldModulType,
                modulType);
    }

    public String getKey() {
        return m_key;
    }

    public void setClearTable(Boolean clearTable) {
        if (clearTable != null && clearTable) {
            firePropertyChange(DataObjectController.CLEAR_TABLE_SELECTION,
                    !clearTable,
                    clearTable);
        }
    }

    public void setKey(String key) {
        if (key == null) {
            key = "";
        }
        String oldKey = m_key;
        m_key = key;
        firePropertyChange(DataObjectController.CHANGE_KEY,
                oldKey,
                key);
    }

    public String getValue() {
        return m_value;
    }

    public void setValue(String value) {
        if (value == null) {
            value = "";
        }
        String oldValue = m_value;
        m_value = value;
        firePropertyChange(DataObjectController.CHANGE_VALUE,
                oldValue,
                value);
    }

    public void setAddKeyToList(Boolean addKey) {
        if (addKey != null && addKey && !m_key.equals("")) {
            for (int i = 0; i < getModel().getRowCount(); i++) {
                if (getModel().getItem(i).getParameter().equals(m_key)) {
                    getModel().removeItem(i);
                    break;
                }
            }
            ParameterModel temp = new ParameterModel(m_key,
                    getValue());
            getModel().addItem(temp);
            firePropertyChange(DataObjectController.ADD_KEY,
                    null,
                    getModel());
        }
    }

    public void setAddKeyToList(String key) {
        if (key != null && !key.equals("")) {
            ParameterModel temp = new ParameterModel(key,
                    "");
            getModel().addItem(temp);
            firePropertyChange(DataObjectController.ADD_KEY,
                    null,
                    getModel());
        }
    }

    public void setAddKeyToList(ParameterModel keyPair) {
        if (keyPair != null) {
            for (int i = 0; i < getModel().getRowCount(); i++) {
                if (getModel().getItem(i).getParameter().equals(keyPair.getParameter())) {
                    getModel().removeItem(i);
                    break;
                }
            }
            getModel().addItem(keyPair);
            firePropertyChange(DataObjectController.ADD_KEY,
                    null,
                    getModel());
            setClearTable(false);
        }
    }

    public void setAddKeyToList(ParameterModel[] keyPairs) {
        if (keyPairs != null) {
            for (int j = 0; j < keyPairs.length; j++) {
                getModel().addItem(keyPairs[j]);
            }
            firePropertyChange(DataObjectController.ADD_KEY,
                    null,
                    getModel());
        }
    }

    public void setRemoveSelectedKeyFromList(Integer selectedKey) {
        if (selectedKey != null && getModel().getRowCount() > selectedKey &&
                selectedKey >= 0) {
            getModel().removeItem(selectedKey);
            firePropertyChange(DataObjectController.REMOVE_SELECTED_KEY,
                    null,
                    getModel());
        }
    }

    public void setRemoveAllKeysFromList(Boolean temp) {
        if (temp != null && temp) {
            getModel().removeAllItems();
            firePropertyChange(DataObjectController.REMOVE_ALL_KEYS,
                    null,
                    getModel());
            setClearTable(true);
        }
    }

    public String getExpectedResult() {
        return m_expectedResult;
    }

    public void setExpectedResult(String expectedResult) {
        if (expectedResult != null) {
            String oldExpectedResult = m_expectedResult;
            m_expectedResult = expectedResult;
            firePropertyChange(DataObjectController.CHANGE_EXPECTED_RESULT,
                    oldExpectedResult,
                    expectedResult);
        }
    }

    public ParameterTableModel getModel() {
        if (m_model == null) {
            setModel(new ParameterTableModel());
        }
        return m_model;
    }

    public void setModel(ParameterTableModel model) {
        if (model != null) {
            ParameterTableModel oldModel = m_model;
            m_model = model;
            firePropertyChange(DataObjectController.ADD_KEY,
                    oldModel,
                    getModel());
        }
    }

    public String getLastWarning() {
        return m_lastWarning;
    }

    public void setLastWarning(String lastWarning) {
        if (lastWarning == null) {
            lastWarning = "";
        }
        String oldVal = m_lastWarning;
        m_lastWarning = lastWarning;
        firePropertyChange(DataObjectController.CHANGE_LAST_WARNING,
                oldVal,
                m_lastWarning);
    }
}
